﻿using Marmat.Forms.Local;
using Marmat.Forms.Views.DataGrid;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Marmat.Forms.Design
{
    /// <summary>
    /// Marmat.Forms.Local.DataGrid 组件的保存方案集合编辑器
    /// </summary>
    public partial class SaveProjectCollentForm : Form
    {

        public Local.DataGrid DataGrid { get; }

        internal IEnumerable<SaveProject> collection;

        public SaveProjectCollentForm(Marmat.Forms.Local.DataGrid dataGrid)
        {
            InitializeComponent();
            this.DataGrid = dataGrid;
            this.collection = new List<SaveProject>();
            foreach (var project in dataGrid.SaveProjects)
            {
                this.listBox1.Items.Add(project);
            }
            foreach (var column in dataGrid.Columns)
            {
                var row = this.dataGrid1.AddNew();
                row["ColumnName"] = column.Name;
                row["Tag"] = column;
            }
            if (this.listBox1.Items.Count > 0)
                this.listBox1.SelectedItem = this.listBox1.Items[0];
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SaveProject project = new SaveProject();
            this.listBox1.Items.Add(project);
            this.listBox1.SelectedItem = project;
            this.SaveProject();
            this.LoadProject(project);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.propertyGrid1.Enabled = true;
            this.dataGrid1.Enabled = true;
            this.SaveProject();
            LoadProject(this.listBox1.SelectedItem as SaveProject);
        }

        private void LoadProject(SaveProject project)
        {
            this.propertyGrid1.SelectedObject = project;
            foreach (Marmat.Forms.Views.DataGrid.Row row in this.dataGrid1.Rows)
            {
                string columnName = row["columnName"].ToString();
                var item = project.Columns.FirstOrDefault(x => x.ViewColumn == columnName);
                row["check"] = item is not null;
                row["DataColumn"] = item?.DataColumn;
                row["primaryKey"] = project.PrimaryKey.Exists(x => x.ViewColumn == columnName);
            }
            this.dataGrid1.AcceptChanges();
        }

        /// <summary>
        /// 保存修改信息
        /// </summary>
        private void SaveProject()
        {
            if (this.propertyGrid1.SelectedObject is SaveProject project && project is not null)
            {
                project.Columns.Clear();
                project.PrimaryKey.Clear();
                foreach (Marmat.Forms.Views.DataGrid.Row row in this.dataGrid1.Rows)
                {
                    string columnName = row["columnName"].ToString();
                    if (Convert.ToBoolean(row["check"]) == true)
                        project.Columns.Add(new SaveProjectColumn() { ViewColumn = columnName, DataColumn = row["DataColumn"]?.ToString() });
                    if (Convert.ToBoolean(row["primaryKey"]) == true)
                    {
                        project.PrimaryKey.Add(new SaveProjectColumn() { ViewColumn = columnName, DataColumn = row["DataColumn"]?.ToString() });
                    }
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedItem is not null)
            {
                this.listBox1.Items.Remove(this.listBox1.SelectedItem);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.SaveProject();
            this.collection = this.listBox1.Items.OfType<SaveProject>();
            this.DialogResult = DialogResult.OK;
        }
    }
}